/**
 * 
 */
package org.jackysoft.struts2.admin;

import java.util.Collection;
import java.util.SortedMap;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.FieldAccess;
import org.jackysoft.entity.FieldAccessPK;
import org.jackysoft.entity.HTMLTemplate;
import org.jackysoft.service.FieldAccessService;
import org.jackysoft.service.FormService;
import org.jackysoft.service.HTMLTemplateService;
import org.jackysoft.struts2.workflow.AbstractWorkflowAction;
import org.jackysoft.util.WorkflowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
/**
 * @author qujiakang
 * 
 */

@Controller
@Namespace("/admin/field")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class FieldManagerAction 
extends AbstractWorkflowAction<FieldAccess> {

	/**
	 * 
	 */
	private FieldAccessPK fieldId;
	private FieldAccess fieldAccess;
	private FieldAccessService service;
	private HTMLTemplate htmlTemplate;
	private Collection<HTMLTemplate> htmlTemplates;
	private HTMLTemplateService tempService;
	private FormService formService;
	private static final long serialVersionUID = 1808615057205658431L;
	private String[] workflowNames  ;
	private SortedMap<Integer, String> stepIdNames ; 
	private String formField;	
	

	@Override
	public String start() throws Exception {
		workflowNames =  workflowService.getWorkflowNames();
		htmlTemplates =  tempService.gets();
		return "start";
	}

	
	public String init() throws Exception {
		stepIdNames = WorkflowUtil.getStepInfomation(workflowService.getWorkflowDescriptor(getWorkflowName().getWorkflowName()));
		htmlTemplate = tempService.get(htmlTemplate.getId());		
		String html = new String(htmlTemplate.getData());
		formField = formService.extractHtmlElementsToJson(html);
		return "init";
	}

	
	@Override
	public String add() throws Exception{
		service.preProceed(fieldAccess);

		return SUCCESS;
	}
	
	
	public String readonly() throws Exception{
		stringList  = service.getReadonly(fieldId);
		return STRINGLIST;
	}

	
	public String[] getWorkflowNames() {
		return workflowNames;
	}

	public void setWorkflowNames(String[] workflowNames) {
		this.workflowNames = workflowNames;
	}

	

	public SortedMap<Integer, String> getStepIdNames() {
		return stepIdNames;
	}

	public void setStepIdNames(SortedMap<Integer, String> stepIdNames) {
		this.stepIdNames = stepIdNames;
	}

	public FormService getFormService() {
		return formService;
	}

	@Autowired
	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public String getFormField() {
		return formField;
	}

	public void setFormField(String formField) {
		this.formField = formField;
	}

	public FieldAccess getFieldAccess() {
		return fieldAccess;
	}

	public void setFieldAccess(FieldAccess fieldAccess) {
		this.fieldAccess = fieldAccess;
	}

	
	public FieldAccessPK getFieldId() {
		return fieldId;
	}

	public void setFieldId(FieldAccessPK fieldId) {
		this.fieldId = fieldId;
	}

	public FieldAccessService getService() {
		return service;
	}

	@Resource
	public void setService(FieldAccessService service) {
		this.service = service;
	}

	public void setHtmlTemplate(HTMLTemplate htmlTemplate) {
		this.htmlTemplate = htmlTemplate;
	}

	public HTMLTemplate getHtmlTemplate() {
		return htmlTemplate;
	}

	@Resource
	public void setTempService(HTMLTemplateService tempService) {
		this.tempService = tempService;
	}

	public void setHtmlTemplates(Collection<HTMLTemplate> htmlTemplates) {
		this.htmlTemplates = htmlTemplates;
	}

	public Collection<HTMLTemplate> getHtmlTemplates() {
		return htmlTemplates;
	}

	
}